from langchain_openai import ChatOpenAI
from globle import envs

from langchain_core.runnables import RunnableParallel,RunnableLambda
from langchain_core.prompts import PromptTemplate as p
from langchain_core.output_parsers import StrOutputParser

llm = ChatOpenAI(
    model="deepseek-chat",  # 或具体模型名
    base_url="https://api.deepseek.com/v1",  # DeepSeek API 地址
    temperature = 2.0,
    max_tokens = 32
)
parser = StrOutputParser()

positive_chain = p.from_template("从积极角度分析{topic}，列举3个优点") | llm | parser
negative_chain = p.from_template("从批判角度分析{topic}，列举3个缺点") | llm | parser

def combine_results(input_dict):
    """将正反两面的分析结果组合成一个更易读的格式"""
    return {
        "combined": f"优点分析:\n{input_dict['positive']}\n\n缺点分析:\n{input_dict['negative']}",
        "topic": input_dict["topic"]
    }

# 构建并行链
together_chain = RunnableParallel({
    "positive": positive_chain,
    "negative": negative_chain,
    "topic": lambda x: x["topic"]
})

sumary_chain = p.from_template("根据{combined}总结一下{topic}") | llm | parser
combine_chain = RunnableLambda(combine_results)

final_chain = together_chain|combine_chain|sumary_chain

result = final_chain.stream({"topic": "远程办公"})
for chunk in result:
    print(chunk, end='')



